/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.ajz.common.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import com.google.common.collect.Maps;

public class SearchFilter {

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public Operator getOperator() {
		return operator;
	}

	public void setOperator(Operator operator) {
		this.operator = operator;
	}

	public enum Operator {
		EQ, LIKE, GT, LT, GTE, LTE,DBTW,IN,NOTIN
	}

	public String fieldName;
	public Object value;
	public Operator operator;

	public SearchFilter(String fieldName, Operator operator, Object value) {
		this.fieldName = fieldName;
		this.value = value;
		this.operator = operator;
	}

	/**
	 * searchParams中key的格式为OPERATOR_FIELDNAME
	 */
	public static Map<String, SearchFilter> parse(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = Maps.newHashMap();

		if (searchParams == null) {
			return filters;
		}
		
		for (Entry<String, Object> entry : searchParams.entrySet()) {
			// 过滤掉空值
			String key = entry.getKey();
			Object value = entry.getValue();
			if(!key.equals("EQ_status") && !key.equals("EQ_IMStatus")){

				if (StringUtils.isBlank((String) value)||"&".equals(value)) {
					continue;
				}
			}else{
				if (StringUtils.isBlank((String) value)||"&".equals(value)) {
					continue;
				}
			}
	

			// 拆分operator与filedAttribute
			String[] names = StringUtils.split(key, "_");
			if (names.length < 2) {
				throw new IllegalArgumentException(key + " is not a valid search filter name");
			}
			String filedName = names[1];
			if(names.length>2){
				filedName=names[1]+"."+names[2];
			}
			Operator operator = Operator.valueOf(names[0]);

			// 创建searchFilter
			SearchFilter filter = new SearchFilter(filedName, operator, value);
			filters.put(key, filter);
		}

		return filters;
	}
	
	public static Sort getSorts(Map<String, Object> searchParams) {        	
		if (searchParams == null) {
			return null;
		}
		List<Order> list = new ArrayList<Order>();
		for (Entry<String, Object> entry : searchParams.entrySet()) {
				
			String key = entry.getKey();
			Object value = entry.getValue();
			if ((value instanceof List)&&((List<?>)value).isEmpty()) {
				continue;
			}else if (!(value instanceof List)&&StringUtils.isBlank(String.valueOf(value))) {
				continue;
			}

			// 拆分operator与filedAttribute
			String[] names = StringUtils.split(key, "_");
			if (names.length != 2) {
				throw new IllegalArgumentException(key + " is not a valid search filter name");
			}
			if(names[0].equals("SORT")){
				  Order order = new Order(Direction.fromString(value.toString()),names[1] );  
				  list.add(order);
//				  searchParams.remove(entry.getKey());
			}
			  
		
        }
		if(list.isEmpty()){
			return null;
		}

		return  new Sort(list);
	}
}
